<?php

namespace app\controller\admin;

use app\common\model\PayOrderModel;
use app\model\AdminAttendanceRecord;
use app\model\AdminAttendanceSys;
use app\model\AdminPayOrder;
use app\model\AdminSchool;
use app\model\AdminStaff;
use app\model\AdminStatisticsAttendance;
use app\model\AdminStatisticsPeople;
use think\facade\Log;
use think\Response;

class TimerTask extends Base {

    /**
     * 统计学校未归已归学生数据
     */
    public function statisticsPeople(): Response {
        $schoolArr = (new AdminSchool())->where('is_delete', '=', '0')->select();
        foreach ($schoolArr as $school) {
            $all_user = AdminStaff::where(['school_id'=>$school->id, 'type'=>AdminStaff::STUDENT, 'is_delete'=>0])->count();
            $out_user = AdminStaff::where(['school_id'=>$school->id, 'type'=>AdminStaff::STUDENT, 'is_delete'=>0, 'last_control_status'=>1])->count();
            $in_user = $all_user - $out_user;
            // 未归学生ID列表
            $out_user_ids = AdminStaff::where(['school_id'=>$school->id, 'type'=>0, 'is_delete'=>0, 'last_control_status'=>1])->column('id');
            AdminStatisticsPeople::create([
                'school_id' => $school->id,
                'statistics_time' => time(),
                'all_user' => $all_user,
                'in_user' => $in_user,
                'out_user' => $out_user,
                'out_user_ids' => implode(":", $out_user_ids)
            ]);
        }
        return $this->buildSuccess();
    }

    /**
     * 考勤统计
     */
    public function statisticsAttendance(): Response {
        $attendanceSysArr = (new AdminAttendanceSys())->where('is_delete', '=', '0')->select();
        foreach ($attendanceSysArr as $attendanceSys) {
            // 统计昨天
            $statisticsAttendanceData = TimerTask::statisticsAttendanceData($attendanceSys,'yesterday');
            $statisticsAttendanceData['statistics_time'] = strtotime("-1 day");
            AdminStatisticsAttendance::create($statisticsAttendanceData);
        }
        return $this->buildSuccess();
    }

    /**
     * @param $attendanceSys 考勤组
     * @param $day 统计时间 yesterday：昨天 today：今天
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function statisticsAttendanceData($attendanceSys, $day) {
//        if ($day == 'yesterday') {
//            $dayDate = strtotime("-1 day");
//        }
//        if ($day == 'today') {
//            $dayDate = date('Y-m-d');
//        }
//        \think\facade\Log::error('校验结果'.AttendanceVerityModel::verityToDay($attendanceSys, $dayDate));
//        if (!AttendanceVerityModel::verityToDay($attendanceSys, $dayDate)){
//            return [
//                'school_id' => $attendanceSys->school_id,
//                'attendance_id' => $attendanceSys->id,
//                'all_user' => 0,
//                'yes_user' => 0,
//                'no_user' => 0,
//                'cd_user' => 0,
//                'zt_user' => 0,
//                'all_number' => 0,
//                'yes_number' => 0,
//                'no_number' => 0,
//                'cd_number' => 0,
//                'zt_number' => 0
//            ];
//        }

        // 总需打卡人数
        $all_user = AdminStaff::where(['school_id'=>$attendanceSys->school_id, 'type'=>0, 'status'=>1, 'is_delete'=>0])
            ->where('grade','in', $attendanceSys->grade, 'OR')->count();
        // 已打卡人数
        $yes_user = AdminAttendanceRecord::where(['attendance_id'=>$attendanceSys->id])->where('status', '<>', 0)
            ->whereDay('create_time', $day)->group('staff_id')->count();
        // 迟到打卡人数
        $cd_user = AdminAttendanceRecord::where(['attendance_id'=>$attendanceSys->id, 'status'=>AdminAttendanceRecord::CD])
            ->whereDay('create_time', $day)->group('staff_id')->count();
        // 早退打卡人数
        $zt_user = AdminAttendanceRecord::where(['attendance_id'=>$attendanceSys->id, 'status'=>AdminAttendanceRecord::ZT])
            ->whereDay('create_time', $day)->group('staff_id')->count();
        // 未打卡人数
        $no_user = $all_user - $yes_user;
        // 总需打卡次数
        $all_number = AdminAttendanceSys::SCOPE_DORMITORY == $attendanceSys->scope ? $all_user : $all_user*2;
        // 已打卡次数
        $yes_number = AdminAttendanceRecord::where(['attendance_id'=>$attendanceSys->id])->where('status', '<>', 0)
            ->whereDay('create_time', $day)->count();
        // 未打卡次数
        $no_number = $all_number - $yes_number;
        // 迟到打卡次数
        $cd_number = AdminAttendanceRecord::where(['attendance_id'=>$attendanceSys->id, 'status'=>AdminAttendanceRecord::CD])
            ->whereDay('create_time', $day)->count();
        // 早退打卡次数
        $zt_number = AdminAttendanceRecord::where(['attendance_id'=>$attendanceSys->id, 'status'=>AdminAttendanceRecord::ZT])
            ->whereDay('create_time', $day)->count();

        return [
            'school_id' => $attendanceSys->school_id,
            'attendance_id' => $attendanceSys->id,
            'all_user' => $all_user,
            'yes_user' => $yes_user,
            'no_user' => $no_user,
            'cd_user' => $cd_user,
            'zt_user' => $zt_user,
            'all_number' => $all_number,
            'yes_number' => $yes_number,
            'no_number' => $no_number,
            'cd_number' => $cd_number,
            'zt_number' => $zt_number
        ];
    }

    /**
     * 处理失败的订单发起重试
     */
    public function payOrderRetry(): Response {
        $payOrderArr = (new AdminPayOrder())->where('order_status', '=', 2)->select();
        foreach ($payOrderArr as $payOrder) {
            $staff = AdminStaff::where(['id'=>$payOrder->staff_id, 'is_delete'=>0])->find();
            if ($staff){
                Log::error('发起失败订单二次处理订单号->'.$payOrder->order_no);
                PayOrderModel::waterElecCharge($payOrder,$staff);
            }
        }
        return $this->buildSuccess();
    }
}